package com.demo.beans;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;

/**
 * @ClassName BeansCreat
 * @Description TODO
 * @Author Windy
 * @Date 2022/8/26 10:44
 * @Version 1.0
 **/
public class BeansCreat {
    private static HashMap<String,Object> map=new HashMap<>();

    static {
        try {
            InputStream is = BeansCreat.class.getResourceAsStream("/beans.properties");
            Properties properties = new Properties();
            properties.load(is);
            Enumeration<Object> keys = properties.keys();
            //判断kes中是否还有值
            while (keys.hasMoreElements()) {
                //将键转为string
                String s = keys.nextElement().toString();
                //获取键对应的值
                String value = properties.getProperty(s);
                Object o = Class.forName(value).newInstance();
                map.put(s,o);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static Object getInstance(String name){
        return map.get(name);
    }
}
